package com.wyc.serviceMap.service.impl;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

import com.wyc.internalCommon.constant.CommonStatusEnum;
import com.wyc.internalCommon.result.ApiResult;
import com.wyc.serviceMap.dto.DistrictDTO;
import com.wyc.serviceMap.mapper.DistrictMapper;
import com.wyc.serviceMap.remote.MapDistrictClient;
import com.wyc.serviceMap.service.DistrictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 地区字典Service业务层处理
 * 
 * @author li
 * @date 2023-03-23
 */
@Service
public class DistrictServiceImpl implements DistrictService
{
    @Autowired
    private DistrictMapper districtDTOMapper;

    @Autowired
    private MapDistrictClient mapDistrictClient;

    /**
     * 查询地区字典
     * 
     * @param addressCode 地区字典主键
     * @return 地区字典
     */
    @Override
    public DistrictDTO selectDistrictDTOByAddressCode(String addressCode)
    {
        return districtDTOMapper.selectDistrictDTOByAddressCode(addressCode);
    }

    /**
     * 查询地区字典列表
     * 
     * @param districtDTO 地区字典
     * @return 地区字典
     */
    @Override
    public List<DistrictDTO> selectDistrictDTOList(DistrictDTO districtDTO)
    {
        return districtDTOMapper.selectDistrictDTOList(districtDTO);
    }

    /**
     * 新增地区字典
     * 
     * @param districtDTO 地区字典
     * @return 结果
     */
    @Override
    public int insertDistrictDTO(DistrictDTO districtDTO)
    {
        return districtDTOMapper.insertDistrictDTO(districtDTO);
    }

    /**
     * 修改地区字典
     * 
     * @param districtDTO 地区字典
     * @return 结果
     */
    @Override
    public int updateDistrictDTO(DistrictDTO districtDTO)
    {
        return districtDTOMapper.updateDistrictDTO(districtDTO);
    }

    /**
     * 批量删除地区字典
     * 
     * @param addressCodes 需要删除的地区字典主键
     * @return 结果
     */
    @Override
    public int deleteDistrictDTOByAddressCodes(String[] addressCodes)
    {
        return districtDTOMapper.deleteDistrictDTOByAddressCodes(addressCodes);
    }

    /**
     * 删除地区字典信息
     * 
     * @param addressCode 地区字典主键
     * @return 结果
     */
    @Override
    public int deleteDistrictDTOByAddressCode(String addressCode)
    {
        return districtDTOMapper.deleteDistrictDTOByAddressCode(addressCode);
    }

    /**
     * 初始化地区字典
     * @return
     */
    @Transactional
    @Override
    public ApiResult initDistrict(String keyWords) {
        // 调用高德api接口获取行政区域数据
        List<DistrictDTO> districtList = mapDistrictClient.initDistrict(keyWords);

        if (districtList.size() == 0 || null == districtList){
            return ApiResult.error(CommonStatusEnum.MAP_DISTRICT_ERROR.getCode(),CommonStatusEnum.MAP_DISTRICT_ERROR.getMsg());
        }

        // 将行政区域数据保存到数据库
        AtomicInteger count = new AtomicInteger();
        List<AtomicInteger> list = districtList.stream().map(item -> {
            DistrictDTO districtDTO = districtDTOMapper.selectDistrictDTOByAddressCode(item.getAddressCode());
            if (null == districtDTO){{
                districtDTOMapper.insert(item);
                count.addAndGet(1);
            }}
            return count;
        }).collect(Collectors.toList());

        return ApiResult.success(list.size());
    }
}
